#! /usr/bin/make -f

ifeq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
export DH_VERBOSE = 1
export PYBUILD_VERBOSE=1
else
export DH_QUIET = 1
endif

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic

export PYBUILD_DESTDIR_python3=debian/python3-pyopencl/
export PYBUILD_DESTDIR_python3-dbg=debian/python3-pyopencl-dbg/

ifneq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
DEB_CXXFLAGS_MAINT_STRIP=-O2
DEB_CXXFLAGS_MAINT_APPEND=-O0
endif

BUILD_DATE=$(shell LC_ALL=C date -u "+%B %d, %Y" -d "@$(SOURCE_DATE_EPOCH)")
SPHINXOPTS:=-D today=\"$(BUILD_DATE)\"
SPHINXOPTS+=-D html_last_updated_fmt=\"$(BUILD_DATE)\"

%:
	dh $@ --with python3,numpy3,sphinxdoc --buildsystem pybuild

override_dh_auto_configure:
	./configure.py --cl-enable-gl

execute_after_dh_auto_build: export HOME:=/tmp
execute_after_dh_auto_build:
	pybuild --build -i python3 -s custom --build-args 'make -C {dir}/doc html'

execute_after_dh_auto_clean:
	$(MAKE) -C doc clean
	$(RM) doc/constants.inc
	$(RM) -r tmp
	$(RM) Makefile siteconf.py
	$(RM) -r $(DEB_SOURCE).egg-info
	$(RM) -r .eggs

# Sphinx documentation is architecture-independent
override_dh_sphinxdoc-arch:

# Do not run test automatically - they require OpenCL-capable hardware
override_dh_auto_test:
	dh_auto_test -- -s custom --test-args="PYTHONPATH={build_dir} cp -r {dir}/test {build_dir} && cd {build_dir}/test && {interpreter} -m pytest --verbosity=2 -k 'not test_event_set_callback' && rm -rf {build_dir}/test"

override_dh_strip:
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -ppython3-pyopencl --dbg-package=python3-pyopencl-dbg
endif


GIT		?= git
GIT_REVISION	?= v$(DEB_VERSION_UPSTREAM)
GIT_SUBMODULES	?= pyopencl/compyte
GIT_URL		?= $(shell sed -rn 's/Repository: //p' debian/upstream/metadata)

get-orig-source: PREFIX = $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM).orig
get-orig-source:
	$(RM) -r $(PREFIX) $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.gz
	$(GIT) clone $(GIT_URL) $(PREFIX)
	cd $(PREFIX) && $(GIT) checkout $(GIT_REVISION) && $(GIT) submodule init $(GIT_SUBMODULES) && $(GIT) submodule update $(GIT_SUBMODULES)
	tar cf - \
		--mtime=@$$(cd $(PREFIX) && $(GIT) show -s --format=%ct) \
		--sort=name --owner=0 --group=0 \
		--exclude='.git*' \
		$(PREFIX) \
		| xz -9 > $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.xz
	$(RM) -r $(PREFIX)
