#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow
export DEB_CPPFLAGS_MAINT_APPEND=-fmacro-prefix-map=$(CURDIR)=.

export PYBUILD_NAME=pygpu
export PYBUILD_SYSTEM=distutils
export PYBUILD_BEFORE_BUILD={interpreter} setup.py build_ext -b {build_dir} -I $(CURDIR)/src -L $(CURDIR)/lib

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

override_dh_auto_clean:
	dh_auto_clean --buildsystem=cmake
	dh_auto_clean
	find $(CURDIR) -type f -name "*.so" -delete
ifeq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
	$(MAKE) -C doc clean
endif

override_dh_auto_configure:
	#force generated files in source to be rebuilt
	rm -f src/gpuarray/types.h src/gpuarray_types.c src/cluda_cuda.h.c src/cluda_opencl.h.c
	dh_auto_configure --buildsystem=cmake
	dh_auto_configure

override_dh_auto_build-arch:
	dh_auto_build --buildsystem=cmake
	dh_auto_build
	# strip build paths from Cython-generated C for reproducibility

override_dh_auto_build-indep: export http_proxy=127.0.0.1:9
override_dh_auto_build-indep: export https_proxy=127.0.0.1:9
override_dh_auto_build-indep:
ifeq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
	#can't build the documentation without first building the library - #901111
	dh_auto_build --buildsystem=cmake
	python3 setup.py build_ext --inplace -I $(CURDIR)/src -L $(CURDIR)/lib
	LD_LIBRARY_PATH=$(CURDIR)/lib PYTHONPATH=$(CURDIR) $(MAKE) -C doc html
endif

# Tests are run in the autopkgtest not here - not all architectures have pocl-opencl-icd
override_dh_auto_test:

override_dh_auto_install-arch:
	dh_auto_install --buildsystem=cmake
	dh_auto_install
	for cfile in `find debian -name "*.c"` ; do echo "stripping paths in $$cfile" ; \
	    sed -i -e 's#/\* "(../)+usr/lib/python3/dist-packages/#/* "/usr/lib/python3/dist-packages/#g' $$cfile ; done

# Do nothing because our only arch: all package is -doc, which uses a .docs file instead
override_dh_auto_install-indep:

override_dh_python3:
	dh_python3
	dh_numpy3
