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

# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

# When updating this variable, do not forget to adapt the build-depends
# in debian/control
LLVM_VERSION=3.8

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

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

export DEB_HOST_MULTIARCH
DEB_HOST_ARCH?=$(shell dpkg-architecture -qDEB_HOST_ARCH)
LLVM_ARCH=$(shell sed < debian/supported-archs \
	-e '/^DEB_ARCH: $(DEB_HOST_ARCH)$$/,/^ *$$/p;d' \
	| sed -e 's/^LLVM_ARCH=//p;d')

ifneq ($(LLVM_ARCH),)
export LLC_HOST_CPU=$(LLVM_ARCH)
endif

ifeq ($(DEB_HOST_ARCH),armhf)
# NEON, optional for Debian, must not be used
export CLANG_FLAGS= -mfpu=vfp3-d16
#export KERNEL_LIB_OPT_FLAGS= -mattr=-neon
export KERNEL_LIB_OPT_FLAGS= -mattr=-neon,+d16,+vfp3,-vfp4,+v7,+aclass,+thumb2
endif

# doc for symbols:
# http://www.eyrie.org/~eagle/journal/2012-01/008.html
# main packaging script based on dh7 syntax
%:
	dh $@  --with autoreconf --parallel --with pkgkde_symbolshelper --with sphinxdoc

.PHONY: override_dh_auto_configure override_dh_shlibdeps override_dh_install
.PHONY: override_dh_auto_test override_dh_makeshlibs

override_dh_autoreconf:
ifeq ($(LLVM_ARCH),)
	$(warning Experimental architecture. Build failure expected)
endif
	@echo Using LLC_HOST_CPU=$${LLC_HOST_CPU}
	-/usr/lib/llvm-$(LLVM_VERSION)/bin/llc -mattr=help
	dh_autoreconf --as-needed autoreconf -- -vif -Wall -Wno-obsolete

override_dh_auto_configure:
	dh_auto_configure -- --enable-icd --disable-static \
		LLVM_CONFIG=/usr/bin/llvm-config-$(LLVM_VERSION)

override_dh_auto_build-indep:
	sphinx-build -N -b html doc/sphinx/source build-doc/html

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

override_dh_install:
	sed -i debian/tmp/etc/OpenCL/vendors/pocl.icd \
		-e 's,.*/\([^/.]*\.so\.[^/.]*\)\.[^/]*,\1,'
	dh_install --list-missing -Xusr/include/CL/ -X.la -Xusr/include/utlist.h

override_dh_auto_test:
	# Failure in testsuite is delayed until symbols handling
	# and all files are logged.
	$(RM) debian/stamp-failed-testsuite
	$(RM) -r debian/testsuite-tmp-home
	mkdir debian/testsuite-tmp-home
	if ! HOME=$(CURDIR)/debian/testsuite-tmp-home dh_auto_test ; then \
		for f in $$(find tests/testsuite.dir -type f | sort) tests/testsuite.log; do \
			echo "******************************************" ;\
			echo "* File: $$f" ;\
			echo "******************************************" ;\
			cat $$f ;\
			echo "******************************************" ;\
		done ;\
		touch debian/stamp-failed-testsuite; \
	fi
	$(RM) -r debian/testsuite-tmp-home

run_dh_makeshlibs:
	dh_makeshlibs

delayed_check_dh_auto_test_result: run_dh_makeshlibs
	if test -f debian/stamp-failed-testsuite ; then \
		echo "***** The testsuite has failed! *****" ; \
		grep '^ .*FAILED' < tests/testsuite.log ; \
		sed -r '/Summary of the failures/,/Skipped tests/{/Skipped tests/d;p};d' tests/testsuite.log ; \
		exit 1 ; \
	fi
	@test -f tests/testsuite.log && echo 'The testsuite has passed all tests.' || echo '*** The testsuite was *NOT* run! ***'

override_dh_makeshlibs: run_dh_makeshlibs delayed_check_dh_auto_test_result

override_dh_gencontrol:
	dh_gencontrol -- -V'LLVM:Version=$(LLVM_VERSION)'


version			 = $(shell dpkg-parsechangelog --show-field Version)
upstream_version	 = $(shell dpkg-parsechangelog --show-field Version | sed -e 's/-[^-]*$$//')
distribution		 = $(shell dpkg-parsechangelog --show-field Distribution)

fix-symbols:
	pkgkde-getbuildlogs
	echo "pkgkde-symbolshelper batchpatch -v $(upstream_version) pocl_$(distribution)_logs/pocl_$(version)_*build"
