#!/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/

	# debhelper's dh_install copies files with "cp --reflink=auto -a"; on the
	# openKylin build hosts this fails as non-root under fakeroot with
	# "cp: cannot create regular file ... Permission denied".  Install files
	# explicitly with plain "cp" (no --reflink) to avoid that code path.
	install -d -m 755 \
		debian/epics-base/usr/bin \
		debian/epics-base/usr/share/epics/dbd \
		debian/epics-dev/usr/include/epics \
		debian/epics-dev/usr/share/pkg-config \
		debian/libepics-perl/usr/lib/$(DEB_HOST_MULTIARCH)/epics

	cp -a $(BIN_DIR)/caRepeater $(BIN_DIR)/caget $(BIN_DIR)/camonitor \
		$(BIN_DIR)/cainfo $(BIN_DIR)/caput $(BIN_DIR)/catime $(BIN_DIR)/acctst \
		$(BIN_DIR)/caConnTest $(BIN_DIR)/casw $(BIN_DIR)/caEventRate $(BIN_DIR)/e_flex \
		$(BIN_DIR)/iocLogServer $(BIN_DIR)/antelope \
		$(BIN_DIR)/pvget $(BIN_DIR)/pvmonitor $(BIN_DIR)/pvput $(BIN_DIR)/pvcall \
		$(BIN_DIR)/pvinfo $(BIN_DIR)/pvlist \
		debian/epics-base/usr/bin/
	cp -a dbd/* debian/epics-base/usr/share/epics/dbd/
	cp -a include/* debian/epics-dev/usr/include/epics/
	cp -a lib/pkgconfig/* debian/epics-dev/usr/share/pkg-config/
	cp -a lib/perl/* debian/libepics-perl/usr/lib/$(DEB_HOST_MULTIARCH)/epics/
	$(RM) debian/libepics-perl/usr/lib/$(DEB_HOST_MULTIARCH)/epics/EpicsHostArch.pl

	install -d -m 755 \
		debian/epics-base/usr/lib/$(DEB_HOST_MULTIARCH) \
		debian/epics-dev/usr/lib/$(DEB_HOST_MULTIARCH) \
		debian/libca4.14.2/usr/lib/$(DEB_HOST_MULTIARCH) \
		debian/libca-dev/usr/lib/$(DEB_HOST_MULTIARCH) \
		debian/libcom3.22.0/usr/lib/$(DEB_HOST_MULTIARCH) \
		debian/libcom-dev/usr/lib/$(DEB_HOST_MULTIARCH)

	cp -a $(REL_LIB_DIR)/libdbCore.so.*     debian/epics-base/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libdbCore.so       debian/epics-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libdbRecStd.so.*   debian/epics-base/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libdbRecStd.so     debian/epics-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libpvAccess.so.*   debian/epics-base/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libpvAccessCA.so.* debian/epics-base/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libpvData.so.*     debian/epics-base/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libpvAccess.so     debian/epics-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libpvAccessCA.so   debian/epics-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libpvData.so       debian/epics-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libca.so.*         debian/libca4.14.2/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libca.so           debian/libca-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libCom.so.*        debian/libcom3.22.0/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp -a $(REL_LIB_DIR)/libCom.so          debian/libcom-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
