#!/usr/bin/make -f

export MAJOR_VER = 20
export MAJOR_PKG = 20

# path to auxiliary build files
AUX_MK_DIR = /usr/share/postgresql-common/server

# include dpkg makefiles
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/vendor.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# strict symbol checking
export DPKG_GENSYMBOLS_CHECK_LEVEL = 4

# Extract server catalog and control file version numbers.
# This information is stored in the packages and used at install time to
# determine if an in-major-version pg_upgradecluster is required.
SERVERCATVERSION=$(shell awk '/^#define CATALOG_VERSION_NO/ { print $$3 }' src/include/catalog/catversion.h)
CONTROLVERSION=$(shell awk '/^#define PG_CONTROL_VERSION/ { print $$3 }' src/include/catalog/pg_control.h)
CATVERSION=$(SERVERCATVERSION)-$(CONTROLVERSION)

# LLVM support
JIT_PKG = $(shell dh_listpackages -p postgresql-$(MAJOR_PKG)-jit)
ifneq ($(JIT_PKG),)
  export LLVM_CONFIG = llvm-config-19
  LLVM_VERSION = $(firstword $(subst ., ,$(shell $(LLVM_CONFIG) --version)))
  WITH_LLVM = enabled
else
  LLVM_VERSION = 0.invalid # mute dpkg error on empty version fields in debian/control
  WITH_LLVM = disabled
endif

# Facilitate hierarchical profile generation on amd64 (#730134)
ifeq ($(DEB_HOST_ARCH),amd64)
  CFLAGS += -fno-omit-frame-pointer
endif

ifneq ($(filter pkg.postgresql.cassert,$(DEB_BUILD_PROFILES)),)
  CONFIGURE_FLAGS += -Dcassert=true
  GENCONTROL_FLAGS += -Vcassert='$${Newline}$${Newline}This package has been built with cassert enabled.'
endif

# hurd is still mostly not SMP, so frequently freezes if more than 3 regression
# tests are run at the same time
ifneq ($(filter hurd%,$(DEB_HOST_ARCH)),)
  export MAX_CONNECTIONS=3
endif

# recipes

%:
	dh $@ --buildsystem=meson

override_dh_autoreconf override_dh_autoreconf_clean override_dh_update_autotools_config:

override_dh_auto_configure:
	dh_auto_configure --buildsystem=meson -- \
		--auto-features=enabled \
		--bindir=/usr/lib/postgresql/$(MAJOR_VER)/bin \
		--datadir=/usr/share/postgresql/$(MAJOR_VER) \
		--includedir=/usr/include/postgresql/ \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		--mandir=/usr/share/postgresql/$(MAJOR_VER)/man \
		--sysconfdir=/etc/postgresql-common \
		-Dbonjour=disabled \
		-Dbsd_auth=disabled \
		-Ddtrace=enabled \
		-Dextra_version=" ($(DEB_VENDOR) $(DEB_VERSION))" \
		-Dllvm=$(WITH_LLVM) \
		-Drpath=false \
		-Dsystem_tzdata=/usr/share/zoneinfo \
		-Duuid=e2fs \
		$(CONFIGURE_FLAGS)

override_dh_auto_build:
	dh_auto_build --buildsystem=meson -- world

# alpha/hppa fail stats tests with postgresql-15
# hurd implemented semaphores only recently and tests still fail a lot
# powerpc fails thread test on postgresql-16
# sh4 lets qemu segfaults when building postgresql-16
# sparc64 fails bin/summarization-and-inprogress-insertion test with postgresql-15
ifneq ($(filter alpha hppa hurd% m68k powerpc sh4 sparc64,$(DEB_HOST_ARCH)),)
override_dh_auto_test-arch:
	-dh_auto_test --buildsystem=meson
endif

override_dh_auto_test-indep:
	# nothing to do

override_dh_auto_install:
	dh_auto_install --buildsystem=meson -- install-world

override_dh_installchangelogs:
	dh_installchangelogs HISTORY

execute_after_dh_install-arch:
	# link README.Debian.gz to postgresql-common
	mkdir -p debian/postgresql-$(MAJOR_PKG)/usr/share/doc/postgresql-$(MAJOR_VER)
	ln -s ../postgresql-common/README.Debian.gz debian/postgresql-$(MAJOR_PKG)/usr/share/doc/postgresql-$(MAJOR_VER)/README.Debian.gz
	
	# assemble perl version of pg_config in libpq-dev
	mkdir -p debian/libpq-dev/usr/bin
	sed -ne '1,/__DATA__/p' $(AUX_MK_DIR)/pg_config.pl > debian/libpq-dev/usr/bin/pg_config
	LC_ALL=C debian/postgresql-client-$(MAJOR_PKG)/usr/lib/postgresql/$(MAJOR_VER)/bin/pg_config | sed -e 's![^ ]*/debian/postgresql-client-$(MAJOR_PKG)!!' >> debian/libpq-dev/usr/bin/pg_config
	LC_ALL=C debian/postgresql-client-$(MAJOR_PKG)/usr/lib/postgresql/$(MAJOR_VER)/bin/pg_config --help >> debian/libpq-dev/usr/bin/pg_config
	chmod 755 debian/libpq-dev/usr/bin/pg_config
	[ "$$(debian/libpq-dev/usr/bin/pg_config --bindir)" = "/usr/lib/postgresql/$(MAJOR_VER)/bin" ]
	
	# remove actual build path from Makefile.global for reproducibility
	sed -i -e "s!^abs_top_builddir.*!abs_top_builddir = /build/postgresql-$(MAJOR_PKG)/build!" \
	       -e "s!^abs_top_srcdir.*!abs_top_srcdir = /build/postgresql-$(MAJOR_PKG)/build/..!" \
	       -e 's!-f\(debug\|file\)-prefix-map=[^ ]* !!g' \
	       debian/postgresql-client-$(MAJOR_PKG)/usr/lib/postgresql/$(MAJOR_VER)/lib/pgxs/src/Makefile.global
	
	# these are shipped in the pl packages
	bash -c "rm -v debian/postgresql-$(MAJOR_PKG)/usr/share/postgresql/$(MAJOR_VER)/extension/*{plperl,plpython,pltcl}*"
	bash -c "rm -v debian/postgresql-$(MAJOR_PKG)/usr/lib/postgresql/$(MAJOR_VER)/lib/*{plperl,plpython,pltcl}*"
	bash -c "rm -rfv debian/postgresql-$(MAJOR_PKG)/usr/lib/postgresql/$(MAJOR_VER)/lib/bitcode/*{plperl,plpython,pltcl}*"
	# shipped in the -jit package
	rm -fv debian/postgresql-$(MAJOR_PKG)/usr/lib/postgresql/$(MAJOR_VER)/lib/llvmjit.so
	
	# record catversion in a file
	echo $(CATVERSION) > debian/postgresql-$(MAJOR_PKG)/usr/share/postgresql/$(MAJOR_VER)/catalog_version

override_dh_compress:
	dh_compress -X.source -X.c
	# compress manpages (excluding debian/tmp/)
	gzip -9n $(CURDIR)/debian/*-*/usr/share/postgresql/*/man/man*/*.[1-9]

execute_after_dh_installdeb-arch:
	# record catversion in preinst
	sed -i -e 's/@CATVERSION@/$(CATVERSION)/' debian/postgresql-$(MAJOR_PKG)/DEBIAN/preinst

override_dh_gencontrol:
	# record catversion in .deb control file
	dh_gencontrol $(EXCLUDE_PACKAGES) -- -Vpostgresql:Catversion=$(CATVERSION) -Vllvm:Version=$(LLVM_VERSION) $(GENCONTROL_FLAGS)

ifneq ($(EXCLUDE_PACKAGES),)
override_dh_builddeb:
	dh_builddeb $(EXCLUDE_PACKAGES)
endif
