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

# 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

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')

ifeq ($(LLVM_ARCH),)
else
export LLC_HOST_CPU=$(LLVM_ARCH)
endif

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

.PHONY: override_dh_auto_configure override_dh_shlibdeps override_dh_install
.PHONY: override_dh_strip override_dh_auto_test override_dh_makeshlibs
override_dh_auto_configure:
ifeq ($(LLVM_ARCH),)
	$(warning Experimental architecture. Build failure expected)
endif
	@echo Using LLC_HOST_CPU=$${LLC_HOST_CPU}
	-/usr/lib/llvm-3.5/bin/llc -mattr=help
	autoreconf -vif -Wall -Wno-obsolete
	dh_auto_configure -- --enable-icd --disable-static \
		LLVM_CONFIG=/usr/bin/llvm-config-3.5

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_strip:
	dh_strip --dbg-package=libpocl-dbg

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

override_dh_makeshlibs:
	dh_makeshlibs
	if test -f debian/stamp-failed-testsuite ; then \
		grep '^ .*FAILED' < tests/testsuite.log ; \
		exit 1 ; \
	fi

fix-symbols:
	pkgkde-getbuildlogs
	echo pkgkde-symbolshelper batchpatch -v 0.10 pocl_unstable_logs/pocl_0.10-9_*build
