#!/usr/bin/make -f

# for hardening you might like to uncomment this:
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@


override_dh_auto_build:
	make CFLAGS="$(shell dpkg-buildflags --get CFLAGS) -fprofile-generate" \
		LDFLAGS="$(shell dpkg-buildflags --get LDFLAGS) -fprofile-generate"\
		CPPFLAGS="$(shell dpkg-buildflags --get CPPFLAGS)"\
		V=1 PREFIX=/usr DESTDIR=$(CURDIR)/debian/tmp -j$(shell nproc)\
		"INSTALL=install --strip-program=true"\
		all test_programs
	for level in $(shell seq 1 12); do \
		./benchmark -$${level} ./lib/deflate_compress.c > /dev/null; \
	done
	make CFLAGS="$(shell dpkg-buildflags --get CFLAGS) -fprofile-use" \
		LDFLAGS="$(shell dpkg-buildflags --get LDFLAGS) -fprofile-use"\
		CPPFLAGS="$(shell dpkg-buildflags --get CPPFLAGS)"\
		V=1 PREFIX=/usr DESTDIR=$(CURDIR)/debian/tmp -j$(shell nproc)\
		"INSTALL=install --strip-program=true"\
		all test_programs

override_dh_auto_install:
	make CFLAGS="$(shell dpkg-buildflags --get CFLAGS) -fprofile-use" \
		LDFLAGS="$(shell dpkg-buildflags --get LDFLAGS) -fprofile-use"\
		CPPFLAGS="$(shell dpkg-buildflags --get CPPFLAGS)"\
		V=1 PREFIX=/usr DESTDIR=$(CURDIR)/debian/tmp -j$(shell nproc)\
		"INSTALL=install --strip-program=true"\
		install

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	SMOKEDATA=$(CURDIR)/README.md WRAPPER= tools/run_tests.sh \
		  -checksum_benchmarks -windows -android -mips \
		  -static_analysis -native
	SMOKEDATA=$(CURDIR)/README.md WRAPPER= sh tools/exec_tests.sh
endif
# mips test requires two hosts;
# when building on mips the native arch tests are still run

override_dh_install:
	dh_install
	d-shlibmove --commit \
		    --multiarch \
		    --devunversioned \
		    --exclude-la \
		    debian/tmp/usr/lib/*.so
