#!/usr/bin/make -f
export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# don't emit NEON instructions whatsoever to prevent FTBFS on machines which
# doesn't support that, see #842143 [thanks to Paul Wise]
ifeq "$(shell dpkg-architecture -q DEB_HOST_ARCH)" "armhf"
export DEB_CFLAGS_MAINT_APPEND = -mfpu=vfpv3
export DEB_CXXFLAGS_MAINT_APPEND = -mfpu=vfpv3
endif

export HOSTNAME =
export PREFIX = /usr

ifeq "$(filter i386 amd64,$(shell dpkg-architecture -q DEB_HOST_ARCH_CPU))" ""
export AFL_NO_X86 = 1
endif

# Keep this in sync with Build-Depends (clang-X, llvm-X-dev)
export LLVM_MAJOR_VERSION = 11
export LLVM_CONFIG = llvm-config-$(LLVM_MAJOR_VERSION)

%:
	dh $@ --with buildinfo

override_dh_auto_build:
	$(MAKE)
	$(MAKE) -C libdislocator
	$(MAKE) -C libtokencap
	$(MAKE) -C llvm_mode

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
#	rm debian/tmp/usr/share/doc/afl++-doc/Changelog.md
#	rm debian/tmp/usr/share/doc/afl++-doc/README.md
	$(MAKE) -C libdislocator install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C libtokencap install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C llvm_mode install DESTDIR=$(CURDIR)/debian/tmp

override_dh_install:
	dh_install
ifneq "$(AFL_NO_X86)" ""
	# Keep only the *-fast* variants on non-x86
	cd debian/afl++/usr/bin/ && rm afl-gcc afl-g++
	cd debian/afl++/usr/lib/afl/ && rm as afl-as
	cd debian/afl++-clang/usr/bin/ && rm afl-clang afl-clang++
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) test
endif

override_dh_gencontrol:
	dh_gencontrol -- -Vllvm-major-version=$(LLVM_MAJOR_VERSION)

override_dh_installdocs:
	dh_installdocs -A README.md -XINSTALL.md

override_dh_compress:
	dh_compress -X.c -X.mp4 -X.sql -XREADME -X.txt

# Exclude afl++-doc from many operations due to embedded binaries
override_dh_buildinfo:
	dh_buildinfo -Nafl++-doc

override_dh_strip_nondeterminism:
	# see #803503
	dh_strip_nondeterminism -Nafl++-doc

override_dh_strip:
	dh_strip -Nafl++-doc

override_dh_makeshlibs:
	dh_makeshlibs -Nafl++-doc 
