From 42b0fe27f3eeb54212474a0f5d9c5b8c2d16307c Mon Sep 17 00:00:00 2001
From: Craig Ringer <craig.ringer@2ndquadrant.com>
Date: Mon, 16 Nov 2020 15:05:06 +0800
Subject: [PATCH v1] De-copy-paste the makefiles

Generate make targets for all versions from a common base and merge
all the duplicate targets.

Accept arguments to override the workdir/outdir using WORKDIR=
since the makefiles like to override rpmbuild's defaults.

Autodetect the appropriate default build target like "build13"
or "buildcommon" on the current directory, where possible.
---
 .gitignore                                  |  14 +++
 rpm/redhat/global/Makefile.global           | 126 +++++++++++++++++---
 rpm/redhat/global/Makefile.global-PG10      | 109 -----------------
 rpm/redhat/global/Makefile.global-PG11      | 109 -----------------
 rpm/redhat/global/Makefile.global-PG12      | 109 -----------------
 rpm/redhat/global/Makefile.global-PG13      | 109 -----------------
 rpm/redhat/global/Makefile.global-PG95      | 110 -----------------
 rpm/redhat/global/Makefile.global-PG96      | 110 -----------------
 rpm/redhat/global/Makefile.global-common    |  85 +++++--------
 rpm/redhat/global/Makefile.global-pgcommon  |  95 +++++++++++++++
 rpm/redhat/global/Makefile.global-verdetect |  65 ++++++++++
 11 files changed, 315 insertions(+), 726 deletions(-)
 create mode 100644 .gitignore
 delete mode 100644 rpm/redhat/global/Makefile.global-PG10
 delete mode 100644 rpm/redhat/global/Makefile.global-PG11
 delete mode 100644 rpm/redhat/global/Makefile.global-PG12
 delete mode 100644 rpm/redhat/global/Makefile.global-PG13
 delete mode 100644 rpm/redhat/global/Makefile.global-PG95
 delete mode 100644 rpm/redhat/global/Makefile.global-PG96
 create mode 100644 rpm/redhat/global/Makefile.global-pgcommon
 create mode 100644 rpm/redhat/global/Makefile.global-verdetect

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 000000000..57571cead
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,14 @@
+*.pdf
+i386
+i586
+i686
+x86_64
+noarch
+*.src.rpm
+*.tar
+*.tar.gz
+*.tar.bz2
+*.tgz
+*.zip
+*.xz
+*.pdf
diff --git a/rpm/redhat/global/Makefile.global b/rpm/redhat/global/Makefile.global
index 90ff9648d..c6bd084c3 100644
--- a/rpm/redhat/global/Makefile.global
+++ b/rpm/redhat/global/Makefile.global
@@ -7,14 +7,101 @@
 # devrim@gunduz.org	   	#
 #################################
 
-prep:
-	if [ -f dead.package ]; then echo "This package is marked as dead. Build won't continue"; exit 1; fi
+ifndef SPECFILE
+$(error Invoke this Makefile via a makefile in a specific package directory)
+endif
 
-	# Update spec file, patches, etc, before running spectool:
+ifndef DIST
+$(error DIST must be defined by including makefile)
+endif
+
+#
+# Make options
+#
+
+#     USE_NETWORK=0
+#       Supress git pull and spectool fetching of dependencies
+#
+USE_NETWORK ?= 1
+
+#     USE_GIT=0
+#       Suppress git fetch or pull from remote, still fetch spectool
+#       files if required.
+#
+USE_GIT ?= 1
+
+#
+#     EXTRA_RPM_DEFINES=
+#       Extra --define arguments to rpmbuild and spectool, e.g.
+#       EXTRA_RPM_DEFINES='--define "foo bar"'
+#
+#       Quoting is important.
+#
+#       There's also an EXTRA_RPM_DEFINESXX where XX is the target suffix
+#       like 13 or 13testing, or COMMON_EXTRA_RPM_DEFINES for the commonxx
+#       targets.
+
+EXTRA_RPM_DEFINES ?=
+
+#
+#     WORKDIR=$(HOME)
+#       Where to create directories based on $(RPMBASEDIRNAME) to put various
+#       build products. At this time there's no way to control where the
+#       various subdirs go.
+#
+#       This default should be changed not to drop things in $HOME
+#
+#       Directory must exist.
+#
+WORKDIR ?= $(HOME)
+
+ifneq ($(USE_NETWORK),1)
+USE_GIT:=0
+endif
+
+git_pull_cmd?=$(info skipping git pull)
+
+# These macros should be defined for any invocation that processes the specfile
+# or srpm, with the first argument as the major ver without . e.g. 96, and
+# second as the major verison with . e.g. 9.6.
+define specfile_defines
+  --define "pgmajorversion $(1)" \
+  --define "pginstdir /usr/pgsql-$(2)" \
+  --define "pgpackageversion $(2)"
+endef
+
+# defines for all rpmbuild invocations, pg-specific or not
+define rpmbuild_defines_base
+    --define "_sourcedir $(CURDIR)" \
+    --define "_specdir $(CURDIR)" \
+    --define "_buildrootdir $(WORKDIR)/rpm$(1)/BUILDROOT" \
+    --define "_builddir $(WORKDIR)/rpm$(1)/BUILD" \
+    --define "_srcrpmdir $(WORKDIR)/rpm$(1)/SRPMS" \
+    --define "_rpmdir $(WORKDIR)/rpm$(1)/RPMS/" \
+    --define "dist $(DIST)"
+endef
+
+# All rpm macros to define when building a pg-version specific package
+define rpmbuild_defines_pg
+    $(call rpmbuild_defines_base,$(1)) \
+    $(call specfile_defines,$(1),$(2))
+endef
+
+
+
+check-dead:
+	@if [ -f dead.package ]; then echo "This package is marked as dead. Build won't continue"; exit 1; fi
+
+ifeq ($(USE_GIT),1)
+# Update spec file, patches, etc, before running spectool
+run-git-pull:
+	@echo Running git pull to refresh the repo. Suppress with USE_GIT=0 .
 	git pull
+else
+run-git-pull:
+	@echo skipping git pull, USE_NETWORK=0 and/or USE_GIT=0 set
+endif
 
-	# Use spectool to download source files, especially tarballs.
-	spectool -g -S $(SPECFILE)
 
 allclean:
 	git clean -dfx
@@ -22,16 +109,23 @@ allclean:
 clean:
 	rm -rf i386/ i586/ i686/ x86_64/ noarch/
 	rm -f *.src.rpm
-	rm -f *.tar *.tar.gz *.tar.bz2 *.tgz *.zip .xz
+	rm -f *.tar *.tar.gz *.tar.bz2 *.tgz *.zip *.xz *.pdf
 
+.PHONY: check-dead run-git-pull run-spectool prep allclean clean
+
+# Try to detect what to build based on makefile path. Note that this won't
+# see paths with symlinks, it sees the path from `realpath .` not the path
+# from `pwd` in the shell.
+#
+specdir = $(abspath $(dir $(abspath $(firstword $(MAKEFILE_LIST)))))
+include ../../../../global/Makefile.global-verdetect
+
+# buildcommon and other common-suffix targets
 include ../../../../global/Makefile.global-common
-include ../../../../global/Makefile.global-PG95
-include ../../../../global/Makefile.global-PG96
-include ../../../../global/Makefile.global-PG10
-include ../../../../global/Makefile.global-PG11-testing
-include ../../../../global/Makefile.global-PG11
-include ../../../../global/Makefile.global-PG12-testing
-include ../../../../global/Makefile.global-PG12
-include ../../../../global/Makefile.global-PG13-testing
-include ../../../../global/Makefile.global-PG13
-include ../../../../global/Makefile.global-PG14-testing
+
+# template for all the version suffix targets like build13 and build13testing
+include ../../../../global/Makefile.global-pgcommon
+
+# Generate version targets like build13 and build13testing
+SUPPORTED_PG_VERSIONS ?= 9.5 9.6 10 11 12 13 14
+$(foreach v,$(SUPPORTED_PG_VERSIONS),$(eval $(call pg_targets,$(v))))
diff --git a/rpm/redhat/global/Makefile.global-PG10 b/rpm/redhat/global/Makefile.global-PG10
deleted file mode 100644
index 96557f46b..000000000
--- a/rpm/redhat/global/Makefile.global-PG10
+++ /dev/null
@@ -1,109 +0,0 @@
-#################################
-# Makefile for PostgreSQL 10	#
-# packaging	  	 	#
-# https://yum.postgresql.org	#
-#			   	#
-# Devrim Gunduz		   	#
-# devrim@gunduz.org	   	#
-#################################
-#			 	#
-#				#
-# build target is for      	#
-# RPM buildfarm		   	#
-#			   	#
-#			   	#
-#################################
-
-
-## PostgreSQL 10
-
-prep10:
-	if [ -f dead.package ]; then echo "This package is marked as dead. Build won't continue"; exit 1; fi
-	# Update spec file, patches, etc, before running spectool:
-	git pull
-	# Use spectool to download source files, especially tarballs.
-	spectool -g -S --define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10" --define "pgpackageversion 10" $(SPECFILE)
-
-build10: bfsrpm10
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "_builddir $(HOME)/rpm10/BUILD" \
-	--define "_srcrpmdir $(HOME)/rpm10/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm10/RPMS/"  \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10" --define "pgpackageversion 10" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-srpm10: prep10
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ."  \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10"  --define "pgpackageversion 10" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfsrpm10: prep10
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm10/SRPMS"  \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10"  --define "pgpackageversion 10" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfnosignsrpm10: prep10
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm10/SRPMS" \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10"  --define "pgpackageversion 10" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-rpm10: prep10
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10" --define "pgpackageversion 10" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nosignbuild10: bfnosignsrpm10
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm10/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm10/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm10/RPMS/" \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10" --define "pgpackageversion 10" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepbuild10:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm10/SRPMS"  \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10" --define "pgpackageversion 10" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm10/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm10/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm10/RPMS/"  \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10" --define "pgpackageversion 10" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nopreprpm10:
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10" --define "pgpackageversion 10" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepsrpm10:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm10/BUILDROOT" \
-	--define "pgmajorversion 10" --define "pginstdir /usr/pgsql-10" --define "pgpackageversion 10" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
diff --git a/rpm/redhat/global/Makefile.global-PG11 b/rpm/redhat/global/Makefile.global-PG11
deleted file mode 100644
index a897890c2..000000000
--- a/rpm/redhat/global/Makefile.global-PG11
+++ /dev/null
@@ -1,109 +0,0 @@
-#################################
-# Makefile for PostgreSQL 11	#
-# packaging	  	 	#
-# https://yum.postgresql.org	#
-#			   	#
-# Devrim Gunduz		   	#
-# devrim@gunduz.org	   	#
-#################################
-#			 	#
-#				#
-# build target is for      	#
-# RPM buildfarm		   	#
-#			   	#
-#			   	#
-#################################
-
-
-## PostgreSQL 11
-
-prep11:
-	if [ -f dead.package ]; then echo "This package is marked as dead. Build won't continue"; exit 1; fi
-	# Update spec file, patches, etc, before running spectool:
-	git pull
-	# Use spectool to download source files, especially tarballs.
-	spectool -g -S --define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11" --define "pgpackageversion 11" $(SPECFILE)
-
-build11: bfsrpm11
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "_builddir $(HOME)/rpm11/BUILD" \
-	--define "_srcrpmdir $(HOME)/rpm11/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm11/RPMS/" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11" --define "pgpackageversion 11" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-srpm11: prep11
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11"  --define "pgpackageversion 11" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfsrpm11: prep11
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm11/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11"  --define "pgpackageversion 11" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfnosignsrpm11: prep11
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm11/SRPMS" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11"  --define "pgpackageversion 11" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-rpm11: prep11
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11" --define "pgpackageversion 11" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nosignbuild11: bfnosignsrpm11
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm11/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm11/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm11/RPMS/" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11" --define "pgpackageversion 11" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepbuild11:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm11/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11" --define "pgpackageversion 11" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm11/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm11/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm11/RPMS/" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11" --define "pgpackageversion 11" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nopreprpm11:
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11" --define "pgpackageversion 11" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepsrpm11:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm11/BUILDROOT" \
-	--define "pgmajorversion 11" --define "pginstdir /usr/pgsql-11" --define "pgpackageversion 11" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
diff --git a/rpm/redhat/global/Makefile.global-PG12 b/rpm/redhat/global/Makefile.global-PG12
deleted file mode 100644
index 0fe86ec24..000000000
--- a/rpm/redhat/global/Makefile.global-PG12
+++ /dev/null
@@ -1,109 +0,0 @@
-#################################
-# Makefile for PostgreSQL 12	#
-# packaging	  	 	#
-# https://yum.postgresql.org	#
-#			   	#
-# Devrim Gunduz		   	#
-# devrim@gunduz.org	   	#
-#################################
-#			 	#
-#				#
-# build target is for      	#
-# RPM buildfarm		   	#
-#			   	#
-#			   	#
-#################################
-
-
-## PostgreSQL 12
-
-prep12:
-	if [ -f dead.package ]; then echo "This package is marked as dead. Build won't continue"; exit 1; fi
-	# Update spec file, patches, etc, before running spectool:
-	git pull
-	# Use spectool to download source files, especially tarballs.
-	spectool -g -S --define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12" --define "pgpackageversion 12" $(SPECFILE)
-
-build12: bfsrpm12
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "_builddir $(HOME)/rpm12/BUILD" \
-	--define "_srcrpmdir $(HOME)/rpm12/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm12/RPMS/" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12" --define "pgpackageversion 12" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-srpm12: prep12
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12"  --define "pgpackageversion 12" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfsrpm12: prep12
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm12/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12"  --define "pgpackageversion 12" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfnosignsrpm12: prep12
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm12/SRPMS" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12"  --define "pgpackageversion 12" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-rpm12: prep12
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12" --define "pgpackageversion 12" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nosignbuild12: bfnosignsrpm12
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm12/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm12/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm12/RPMS/" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12" --define "pgpackageversion 12" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepbuild12:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm12/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12" --define "pgpackageversion 12" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm12/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm12/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm12/RPMS/" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12" --define "pgpackageversion 12" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nopreprpm12:
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12" --define "pgpackageversion 12" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepsrpm12:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm12/BUILDROOT" \
-	--define "pgmajorversion 12" --define "pginstdir /usr/pgsql-12" --define "pgpackageversion 12" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
diff --git a/rpm/redhat/global/Makefile.global-PG13 b/rpm/redhat/global/Makefile.global-PG13
deleted file mode 100644
index a81a85949..000000000
--- a/rpm/redhat/global/Makefile.global-PG13
+++ /dev/null
@@ -1,109 +0,0 @@
-#################################
-# Makefile for PostgreSQL 13	#
-# packaging	  	 	#
-# https://yum.postgresql.org	#
-#			   	#
-# Devrim Gunduz		   	#
-# devrim@gunduz.org	   	#
-#################################
-#			 	#
-#				#
-# build target is for      	#
-# RPM buildfarm		   	#
-#			   	#
-#			   	#
-#################################
-
-
-## PostgreSQL 13
-
-prep13:
-	if [ -f dead.package ]; then echo "This package is marked as dead. Build won't continue"; exit 1; fi
-	# Update spec file, patches, etc, before running spectool:
-	git pull
-	# Use spectool to download source files, especially tarballs.
-	spectool -g -S --define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" $(SPECFILE)
-
-build13: bfsrpm13
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "_builddir $(HOME)/rpm13/BUILD" \
-	--define "_srcrpmdir $(HOME)/rpm13/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm13/RPMS/" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-srpm13: prep13
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13"  --define "pgpackageversion 13" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfsrpm13: prep13
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm13/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13"  --define "pgpackageversion 13" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfnosignsrpm13: prep13
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm13/SRPMS" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13"  --define "pgpackageversion 13" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-rpm13: prep13
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nosignbuild13: bfnosignsrpm13
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm13/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm13/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm13/RPMS/" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepbuild13:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm13/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm13/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm13/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm13/RPMS/" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nopreprpm13:
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepsrpm13:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm13/BUILDROOT" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
diff --git a/rpm/redhat/global/Makefile.global-PG95 b/rpm/redhat/global/Makefile.global-PG95
deleted file mode 100644
index 1da07a0d5..000000000
--- a/rpm/redhat/global/Makefile.global-PG95
+++ /dev/null
@@ -1,110 +0,0 @@
-#################################
-# Makefile for PostgreSQL 95	#
-# packaging	  	 	#
-# https://yum.postgresql.org	#
-#			   	#
-# Devrim Gunduz		   	#
-# devrim@gunduz.org	   	#
-#################################
-#			 	#
-#				#
-# build target is for      	#
-# RPM buildfarm		   	#
-#			   	#
-#			   	#
-#################################
-
-
-## PostgreSQL 95
-
-prep95:
-	if [ -f dead.package ]; then echo "This package is marked as dead. Build won't continue"; exit 1; fi
-	# Update spec file, patches, etc, before running spectool:
-	git pull
-	# Use spectool to download source files, especially tarballs.
-	spectool -g -S --define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5" --define "pgpackageversion 9.5" $(SPECFILE)
-
-build95: bfsrpm95
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "_builddir $(HOME)/rpm95/BUILD" \
-	--define "_srcrpmdir $(HOME)/rpm95/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm95/RPMS/" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5" --define "pgpackageversion 9.5" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-srpm95: prep95
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5"  --define "pgpackageversion 9.5" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfsrpm95: prep95
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm95/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5"  --define "pgpackageversion 9.5" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfnosignsrpm95: prep95
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm95/SRPMS" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5"  --define "pgpackageversion 9.5" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-rpm95: prep95
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5" --define "pgpackageversion 9.5" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nosignbuild95: bfnosignsrpm95
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm95/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm95/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm95/RPMS/" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5" --define "pgpackageversion 9.5" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepbuild95:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm95/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5" --define "pgpackageversion 9.5" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm95/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm95/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm95/RPMS/" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5" --define "pgpackageversion 9.5" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nopreprpm95:
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5" --define "pgpackageversion 9.5" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepsrpm95:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm95/BUILDROOT" \
-	--define "pgmajorversion 95" --define "pginstdir /usr/pgsql-9.5" --define "pgpackageversion 9.5" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
diff --git a/rpm/redhat/global/Makefile.global-PG96 b/rpm/redhat/global/Makefile.global-PG96
deleted file mode 100644
index 96e25a19b..000000000
--- a/rpm/redhat/global/Makefile.global-PG96
+++ /dev/null
@@ -1,110 +0,0 @@
-#################################
-# Makefile for PostgreSQL 96	#
-# packaging	  	 	#
-# https://yum.postgresql.org	#
-#			   	#
-# Devrim Gunduz		   	#
-# devrim@gunduz.org	   	#
-#################################
-#			 	#
-#				#
-# build target is for      	#
-# RPM buildfarm		   	#
-#			   	#
-#			   	#
-#################################
-
-
-## PostgreSQL 96
-
-prep96:
-	if [ -f dead.package ]; then echo "This package is marked as dead. Build won't continue"; exit 1; fi
-	# Update spec file, patches, etc, before running spectool:
-	git pull
-	# Use spectool to download source files, especially tarballs.
-	spectool -g -S --define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6" --define "pgpackageversion 9.6" $(SPECFILE)
-
-build96: bfsrpm96
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "_builddir $(HOME)/rpm96/BUILD" \
-	--define "_srcrpmdir $(HOME)/rpm96/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm96/RPMS/" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6" --define "pgpackageversion 9.6" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-srpm96: prep96
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6"  --define "pgpackageversion 9.6" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfsrpm96: prep96
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm96/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6"  --define "pgpackageversion 9.6" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-bfnosignsrpm96: prep96
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm96/SRPMS" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6"  --define "pgpackageversion 9.6" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-rpm96: prep96
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6" --define "pgpackageversion 9.6" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nosignbuild96: bfnosignsrpm96
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm96/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm96/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm96/RPMS/" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6" --define "pgpackageversion 9.6" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepbuild96:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpm96/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6" --define "pgpackageversion 9.6" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpm96/BUILD" \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpm96/SRPMS" \
-	--define "_rpmdir $(HOME)/rpm96/RPMS/" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6" --define "pgpackageversion 9.6" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-nopreprpm96:
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6" --define "pgpackageversion 9.6" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-noprepsrpm96:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpm96/BUILDROOT" \
-	--define "pgmajorversion 96" --define "pginstdir /usr/pgsql-9.6" --define "pgpackageversion 9.6" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
diff --git a/rpm/redhat/global/Makefile.global-common b/rpm/redhat/global/Makefile.global-common
index 16a9cac35..4dc64851d 100644
--- a/rpm/redhat/global/Makefile.global-common
+++ b/rpm/redhat/global/Makefile.global-common
@@ -8,64 +8,41 @@
 #################################
 
 ## PostgreSQL common build targets
+#
+# Build packages that aren't specific to a particular postgres version.
+#
 
-commonbfsrpm: prep
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpmcommon/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpmcommon/BUILDROOT" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13"  --define "pgpackageversion 13" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
+# Fake postgres version to pass in case a specfile wants a particular pg
+# version macro. We should get rid of this and replace it with an rpm error
+# macro later.
+#
+PGVERSION_FOR_COMMON ?= 13
 
-commonbuild: commonbfsrpm
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpmcommon/BUILDROOT" \
-	--define "_builddir $(HOME)/rpmcommon/BUILD" \
-	--define "_srcrpmdir $(HOME)/rpmcommon/SRPMS" \
-	--define "_rpmdir $(HOME)/rpmcommon/RPMS/" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
+define rpmbuild_defines_common
+    $(call rpmbuild_defines_base,common), \
+    $(call specfile_defines,$(PGVERSION_FOR_COMMON),$(PGVERSION_FOR_COMMON))
+endef
 
-commonrpm: prep
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpmcommon/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
+COMMON_EXTRA_RPM_DEFINES ?= $(EXTRA_RPM_DEFINES)
 
-commonnoprepbuild:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir $(HOME)/rpmcommon/SRPMS" \
-	--define "_buildrootdir $(HOME)/rpmcommon/BUILDROOT" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
-
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(HOME)/rpmcommon/BUILD" \
-	--define "_buildrootdir $(HOME)/rpmcommon/BUILDROOT" \
-	--define "_srcrpmdir $(HOME)/rpmcommon/SRPMS" \
-	--define "_rpmdir $(HOME)/rpmcommon/RPMS/" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
-
-commonnopreprpm:
-	rpmbuild --define "_sourcedir $(PWD)" \
-	--define "_specdir $(PWD)" \
-	--define  "_builddir $(PWD)" \
-	--define "_buildrootdir $(HOME)/rpmcommon/BUILDROOT" \
-	--define "_srcrpmdir $(PWD)" \
-	--define "_rpmdir $(PWD)" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "dist $(DIST)" -bb $(SPECFILE)
+commonprep: check-dead run-git-pull
+	spectool -g $(call specfile_defines,$(PGVERSION_FOR_COMMON),$(PGVERSION_FOR_COMMON)) -S $(SPECFILE)
 
 commonnoprepsrpm:
-	rpmbuild --define "_sourcedir ." --define "_specdir ." \
-	--define "_builddir ." --define "_srcrpmdir ." \
-	--define "_buildrootdir $(HOME)/rpmcommon/BUILDROOT" \
-	--define "pgmajorversion 13" --define "pginstdir /usr/pgsql-13" --define "pgpackageversion 13" \
-	--define "_rpmdir ." --define "dist $(DIST)" --nodeps -bs $(SPECFILE)
+	rpmbuild $(call rpmbuild_defines_common) --nodeps -bs $(SPECFILE)
 
+commonnopreprpm:
+	rpmbuild $(call rpmbuild_defines_common) -bb $(SPECFILE)
+
+commonsrpm: commonprep commonnoprepsrpm
+
+commonrpm: commonprep commonnopreprpm
+
+commonbuild: commonsrpm commonnopreprpm
+
+commonnoprepbuild: commonnoprepsrpm commonnopreprpm
+
+# Legacy targets
+commonbfsrpm: commonprep commonnoprepsrpm
+commonbfnosignsrpm: commonprep commonnoprepsrpm
+commonnosignbuild: commonbfnosignsrpm commonnopreprpm
diff --git a/rpm/redhat/global/Makefile.global-pgcommon b/rpm/redhat/global/Makefile.global-pgcommon
new file mode 100644
index 000000000..19fbed423
--- /dev/null
+++ b/rpm/redhat/global/Makefile.global-pgcommon
@@ -0,0 +1,95 @@
+#################################
+# Makefile for PostgreSQL	#
+# packaging	  	 	#
+# https://yum.postgresql.org	#
+#			   	#
+# Devrim Gunduz		   	#
+# devrim@gunduz.org	   	#
+#################################
+#			 	#
+#				#
+# build target is for      	#
+# RPM buildfarm		   	#
+#			   	#
+#			   	#
+#################################
+
+
+## PostgreSQL version builds
+##
+## This is eval'd for each target version
+##
+## The nosign variants are legacy
+##
+## You usually just want "buildNN" which does the lot, e.g. build13
+
+# All rpm macros to define when building a pg-version specific package
+define rpmbuild_defines_pg
+    $(call rpmbuild_defines_base,$(1)) \
+    $(call specfile_defines,$(2),$(3))
+endef
+
+# Macro to create version specific targets.
+#
+#   1: target suffix e.g. 13 or 96 or 13testing
+#   2: pg major without . 96 or 13
+#   3: pg major with . e.g. 9.6 or 13
+#   4: variant identifier, which is usually the same as the additional target
+#      suffix like "testing" but can be whatever you want. You can test this in
+#      the expanded targets to override behaviour.
+#
+# You can also define some Make variables to alter behaviour here:
+#
+#     EXTRA_RPM_DEFINES$(1)=
+#       Set to a list of extra --define arguments to pass to spectool and rpmbuild.
+#       e.g. EXTRA_RPM_DEFINES13='--define "foo bar"'
+#
+#       The quoting is important.
+#
+#     EXTRA_RPM_DEFINES=
+#       The same thing but applies to all builds not version/variant specific
+#
+
+define pg_targets_base
+
+EXTRA_RPM_DEFINES$(1) ?= $(EXTRA_RPM_DEFINES)
+
+prep$(1): check-dead run-git-pull
+	spectool -g $(call specfile_defines,$(2),$(3)) $$(EXTRA_RPM_DEFINES$(1)) $(SPECFILE)
+
+noprepsrpm$(1):
+	rpmbuild $(call rpmbuild_defines_pg,$(1),$(2),$(3)) $$(EXTRA_RPM_DEFINES$(1)) --nodeps -bs $(SPECFILE)
+
+nopreprpm$(1):
+	rpmbuild $(call rpmbuild_defines_pg,$(1),$(2),$(3)) $$(EXTRA_RPM_DEFINES$(1)) -bb $(SPECFILE)
+
+srpm$(1): prep$(1) noprepsrpm$(1)
+
+rpm$(1): prep$(1) nopreprpm$(1)
+
+build$(1): srpm$(1) nopreprpm$(1)
+
+noprepbuild$(1): noprepsrpm$(1) nopreprpm$(1)
+
+# Legacy targets
+bfsrpm$(1): prep$(1) noprepsrpm$(1)
+bfnosignsrpm$(1): prep$(1) noprepsrpm$(1)
+nosignbuild$(1): bfnosignsrpm$(1) nopreprpm$(1)
+
+endef
+
+# Single argument is major version with . e.g 9.6
+# Produces make targets like:
+#
+#     build96
+#     build96testing
+#
+# You can invoke $(pg_targets_base) directly instead for custom
+# variants, e.g.
+#
+#     $(eval $(call pg_targets_base,14beta,14,14))
+#
+define pg_targets
+$(call pg_targets_base,$(subst .,,$(1)),$(subst .,,$(1)),$(1),)
+$(call pg_targets_base,$(subst .,,$(1))testing,$(subst .,,$(1)),$(1),testing)
+endef
diff --git a/rpm/redhat/global/Makefile.global-verdetect b/rpm/redhat/global/Makefile.global-verdetect
new file mode 100644
index 000000000..66c92b8af
--- /dev/null
+++ b/rpm/redhat/global/Makefile.global-verdetect
@@ -0,0 +1,65 @@
+#
+# Makefile-part for automatic version detection of target
+# builds.
+#
+
+ifndef specdir
+$(error set specdir to dir containing the Makefile and .specfile before including)
+endif
+
+pkg_os=$(notdir $(specdir))
+pkg_name=$(notdir $(abspath $(dir $(specdir))))
+common=$(notdir $(abspath $(dir $(abspath $(dir $(specdir))))))
+ifeq ($(common),common)
+  .DEFAULT_GOAL=commonbuild
+  ifneq ($(PGVERSION),)
+    $(warning PGVERSION defined, but this package is in /common and not version specific)
+  endif
+else
+  ifeq ($(common),non-common)
+    # non-common can only set a default target if we know the target postgres
+    # version from the pkgname, or if it's set in the Makefile or environment,
+    # otherwise you have to specify it. For now only detect it for postgresql-
+    # packages.
+    ifeq ($(findstring postgresql-,$(pkg_name)),postgresql-)
+      PGVERSION ?= $(subst postgresql-,,$(pkg_name))
+      $(info Auto-detected target PGVERSION=$(PGVERSION))
+    endif
+    ifneq ($(PGVERSION),)
+      .DEFAULT_GOAL=build$(subst .,,$(PGVERSION))
+    else
+      error_default_message=need a target postgres version, specify PGVERSION $(newline) $(newline) $(usage)
+      .DEFAULT_GOAL=error_default
+    endif
+  else
+    error_default_message=failed to detect /common/ or /non-common/ in expected part of path $(specdir), got $(common) instead. No default target picked. $(newline) $(newline) $(usage)
+    .DEFAULT_GOAL=error_default
+  endif
+endif
+
+# Fake default goal to run when we can't decide what to run
+error_default:
+	$(error $(error_default_message))
+
+# If you are debugging the detection above:
+#$(info Detected from path: pkg_os=$(pkg_os) pkg_name=$(pkg_name) default_goal: $(.DEFAULT_GOAL))
+
+define newline
+
+
+endef
+
+define usage
+Run "make build13" where 13 is the target postgres version
+or "make commonbuild" for packages that are shared in the
+/common/ directories. You can also use supported suffixes
+like 13testing .
+
+Don't know what to do, exiting.
+endef
+
+# This should autodetect instead
+build_detect:
+	$(info $(usage))
+	@exit 0
+
-- 
2.26.2

