#! /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-pycuda/
export PYBUILD_DESTDIR_python3-dbg=debian/python3-pycuda-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 --boost-python-libname=boost_python \
		--boost-thread-libname=boost_thread \
		--cuda-root=/usr --cuda-enable-gl

override_dh_installdocs:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C doc html SPHINXOPTS="$(SPHINXOPTS)"
	dh_installdocs
endif

# Sphinx documentation is architecture-independent
override_dh_sphinxdoc-arch:

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

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


GIT		?= git
GIT_REVISION	?= v$(DEB_VERSION_UPSTREAM)
GIT_SUBMODULES	?= pycuda/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)
	# remove non-free examples
	$(RM) $(PREFIX)/examples/demo_cdpSimplePrint.py
	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)
