#! /usr/bin/make -f

#export DH_VERBOSE = 1
#export DH_BUILD_DDEBS = 1

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

export PYBUILD_DESTDIR_python2=debian/python-pyopencl/
export PYBUILD_DESTDIR_python2-dbg=debian/python-pyopencl-dbg/
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 python2,python3,sphinxdoc --buildsystem pybuild

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

override_dh_auto_install:
	dh_auto_install
	dh_numpy
	dh_numpy3

override_dh_installdocs:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	pybuild --build -i pythonX.Y -s custom --build-args 'make -C {dir}/doc html'
	dh_installdocs
endif

# Sphinx documentation is architecture-independent
override_dh_sphinxdoc-arch:

# Do not run test automatically - they require OpenCL-capable hardware
override_dh_auto_test:

override_dh_compress:
	dh_compress --all -X.py -X.txt -X.js -X.rst -X.json -X.doctree -Xobjects.inv

override_dh_strip:
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -ppython-pyopencl --dbg-package=python-pyopencl-dbg
#	dh_strip -ppython-pyopencl --ddeb-migration='python-pyopencl-dbg (<< 2015.1-1)'
	dh_strip -ppython3-pyopencl --dbg-package=python3-pyopencl-dbg
#	dh_strip -ppython3-pyopencl --ddeb-migration='python3-pyopencl-dbg (<< 2015.1-1)'
endif


GIT_REVISION=v2017.2.2
GIT_SUBMODULES=pyopencl/compyte
GIT_URL=https://git.tiker.net/trees/pyopencl.git

get-orig-source:
	rm -rf $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM) $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.gz
	git clone $(GIT_URL) $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)
	cd $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM) && git submodule init $(GIT_SUBMODULES) && git submodule update $(GIT_SUBMODULES) && git checkout $(GIT_REVISION)
	rm -rf $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/.git*
	rm -rf $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/doc/.gitignore $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/examples/.gitignore
	rm -rf $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/$(GIT_SUBMODULES)/.git $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/$(GIT_SUBMODULES)/.gitignore
	tar caf $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.xz $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)
	rm -rf $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)

