#! /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:
	test ! -d pycuda/compyte || rmdir pycuda/compyte
	test -h pycuda/compyte || ln -s ../compyte pycuda/compyte
	./configure.py --boost-python-libname=boost_python \
		--boost-thread-libname=boost_thread \
		--cuda-root=/usr --cuda-enable-gl

execute_after_dh_auto_build:
	$(MAKE) -C doc html SPHINXOPTS="$(SPHINXOPTS)"

execute_after_dh_auto_clean:
	$(MAKE) -C doc clean
	$(RM) Makefile siteconf.py
	$(RM) -r $(DEB_SOURCE).egg-info
	test ! -h pycuda/compyte || $(RM) pycuda/compyte

# 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$(subst ~dfsg,,$(DEB_VERSION_UPSTREAM))
GIT_URL		?= $(shell sed -rn 's/Repository: //p' debian/upstream/metadata)

# you can create the main tarball with uscan --download-current-version
create-compyte-tarball: PREFIX = $(DEB_SOURCE)-$(GIT_REVISION:v%=%).orig
create-compyte-tarball:
	$(GIT) clone $(GIT_URL) $(PREFIX)
	cd $(PREFIX) && $(GIT) checkout $(GIT_REVISION) && $(GIT) submodule init $(DEB_SOURCE)/compyte && $(GIT) submodule update $(DEB_SOURCE)/compyte
	( cd $(PREFIX)/$(DEB_SOURCE)/compyte && $(GIT) archive --format=tar --prefix=$(PREFIX)-compyte/ HEAD ) \
		| xz -9 > $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig-compyte.tar.xz
	$(RM) -r $(PREFIX)
