#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

SOVERSION	 = 7.0

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DPKG_EXPORT_BUILDFLAGS	 = yes
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

# Upstream does not use CPPFLAGS
CFLAGS	+= $(CPPFLAGS)

export PAPI_ROCM_ROOT=/usr
export PAPI_ROCMSMI_ROOT=$(PAPI_ROCM_ROOT)

COMPONENTS	 = sde
COMPONENTS	+= $(if $(wildcard /usr/include/rocm_smi/rocm_smi.h),rocm_smi)

%:
	dh $@ --sourcedirectory=src

override_dh_auto_configure:
	dh_auto_configure --sourcedirectory=src -- \
		--with-static-lib=yes \
		--with-shared-lib=yes \
		--with-shlib-tools \
		--with-components="$(COMPONENTS)" \
		--with-perf-events \
		--with-pfm-incdir=/usr/include \
		--with-pfm-libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		CC=$(CC)
	# for reproducibility
	sed -r -i '/^[CF]FLAGS/s/-fdebug-prefix-map=\S*\s*//' \
		src/ctests/Makefile.target \
		src/ftests/Makefile.target \
		src/testlib/Makefile.target \
		src/validation_tests/Makefile.target \
		src/components/Makefile_comp_tests.target

override_dh_auto_test:
	-env LD_LIBRARY_PATH=$(CURDIR)/src:$(LD_LIBRARY_PATH) dh_auto_test

override_dh_auto_install:
	mkdir -p debian/tmp/usr/bin
	dh_auto_install --sourcedirectory=src -- install-all
	mv debian/tmp/usr/bin/papi_hl_output_writer.py debian/tmp/usr/bin/papi_hl_output_writer
	# delete example binaries
	find debian/tmp/usr/share/papi -perm -100 -type f -delete
	find debian/tmp/usr/share/papi -name \*.a -type f -delete
	find debian/tmp/usr/share/papi -name Makefile -type f -print0 | \
		xargs -0r sed -i -e 's@ [^ ]*-f\(file\|debug\)-prefix-map=[^ ]*@@g'

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3

override_dh_makeshlibs:
	dh_makeshlibs -a -- -c4

override_dh_shlibdeps:
	dh_shlibdeps -a -- --warnings=7

override_dh_installchangelogs:
	dh_installchangelogs RELEASENOTES.txt

override_dh_gencontrol:
	dh_gencontrol -- \
		-V'papi:SOVERSION=$(SOVERSION)'


TEMPLATES	:= $(wildcard debian/libpapiSOVERSION.*)
GENERATED	 = $(subst SOVERSION,$(SOVERSION),$(TEMPLATES))

libpapi$(SOVERSION).%: libpapiSOVERSION.%
	sed 's/@SOVERSION@/$(SOVERSION)/g' $< > $@

build-generated: $(GENERATED) ;
clean-generated:
	$(RM) $(GENERATED)

override_dh_auto_configure: build-generated
clean: clean-generated
