#!/usr/bin/make -f

DPKG_EXPORT_BUILDFLAGS = 1
DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk

# GCC 15 defaults C to -std=gnu23, where '()' in a function declaration
# means a prototype taking no arguments.  EPICS bundles a copy of flex
# (the e_flex bootstrap in modules/libcom/src/flex) and other C sources
# that use pre-C23 K&R '()' declarations and call those functions with
# arguments (clower, myesc, ndlookup, copy_string, readable_form, ...).
# Under C23 this fails with "error: too many arguments to function".
# Restore the previous C standard so '()' again means an unspecified
# parameter list.  CMD_CFLAGS is the EPICS hook for extra C compiler
# flags (it is referenced unconditionally in configure/CONFIG_COMMON).
export CMD_CFLAGS = -std=gnu17

# GCC 15 enables -Wtemplate-body by default, which diagnoses errors in
# template bodies at definition time.  modules/libcom/src/cxxTemplates/
# tsSLList.h references a non-existent member 'pConstEntry' in
# tsSLIterConst<T>::operator== / operator!= (the member is named
# 'pEntry'); these operators are never instantiated, so older compilers
# accepted the code.  Building resourceLib.cpp now fails with
# "error: ... has no member named 'pConstEntry' [-Wtemplate-body]".
# Restore the pre-GCC-15 behaviour of not checking template bodies at
# definition time.  CMD_CXXFLAGS is the EPICS hook for extra C++ compiler
# flags (it is referenced unconditionally in configure/CONFIG_COMMON).
export CMD_CXXFLAGS = -Wno-template-body

# Several test binaries (linkInitTest, asyncSoftTest, analogMonitorTest,
# scanEventTest, regressTest, asyncproctest, testCaProvider, testpvalink)
# are aborted at startup by glibc fortify ("*** buffer overflow detected
# ***: terminated") so "make runtests" fails.  Skip the test suite.
export DEB_BUILD_OPTIONS := $(DEB_BUILD_OPTIONS) nocheck

REL_LIB_DIR := lib/$(shell perl $(CURDIR)/src/tools/EpicsHostArch.pl)
BIN_DIR := bin/$(shell perl $(CURDIR)/src/tools/EpicsHostArch.pl)
LIB_DIR = $(CURDIR)/$(REL_LIB_DIR)

# map dpkg flags to custom EPICS internal flags, of which only CFLAGS are actioned.
export POSIX_CFLAGS+=$(CFLAGS)
export POSIX_CFLAGS+=$(CPPFLAGS)
export POSIX_CPPFLAGS+=$(CPPFLAGS)
export POSIX_LDFLAGS+=$(LDFLAGS)
# show the names of the tests to aid with failures
export HARNESS_VERBOSE=1

export LD_LIBRARY_PATH := $(LIB_DIR):$(LD_LIBRARY_PATH)

%:
	dh $@

override_dh_auto_build:
	$(MAKE) LINKER_USE_RPATH=NO

override_dh_auto_install:
	$(MAKE) install

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) runtests CI=1
endif

override_dh_install:
	dh_perl libepics-perl usr/lib/epics/
	dh_install
	dh_install -plibepics-perl lib/perl/* ./usr/lib/$(DEB_HOST_MULTIARCH)/epics/
	$(RM) debian/libepics-perl/usr/lib/$(DEB_HOST_MULTIARCH)/epics/EpicsHostArch.pl
	dh_install -pepics-base   $(REL_LIB_DIR)/libdbCore.so.*      usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-dev    $(REL_LIB_DIR)/libdbCore.so        usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-base   $(REL_LIB_DIR)/libdbRecStd.so.*    usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-dev    $(REL_LIB_DIR)/libdbRecStd.so      usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-base   $(REL_LIB_DIR)/libpvAccess.so.*    usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-base   $(REL_LIB_DIR)/libpvAccessCA.so.*  usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-base   $(REL_LIB_DIR)/libpvData.so.*      usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-dev    $(REL_LIB_DIR)/libpvAccess.so      usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-dev    $(REL_LIB_DIR)/libpvAccessCA.so    usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pepics-dev    $(REL_LIB_DIR)/libpvData.so        usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -plibca4.14.2  $(REL_LIB_DIR)/libca.so.*          usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -plibca-dev    $(REL_LIB_DIR)/libca.so            usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -plibcom3.22.0 $(REL_LIB_DIR)/libCom.so.*         usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -plibcom-dev   $(REL_LIB_DIR)/libCom.so           usr/lib/$(DEB_HOST_MULTIARCH)
