#!/usr/bin/make -f

DH_OPTIONS=-Bbuild

source=$(shell dpkg-parsechangelog -S Source)

# This is the current default version, which we may have to conflict against
DEFAULT_GCC_VERSION  := 8

# This is the version we want to build starpu against (may have to change
# according to plugin support)
GCC_VERSION          := 7

# These are the best versions that nvcc supports.
CONTRIB_GCC_VERSION  := 7
CONTRIB_CLANG_VERSION  := 5.0

ifeq ($(source),starpu)
#CC=gcc-$(GCC_VERSION)
#CXX=g++-$(GCC_VERSION)
#F77=gfortran-$(GCC_VERSION)
else
# nvcc sometimes chokes on some gcc features
CC=gcc-$(CONTRIB_GCC_VERSION)
CXX=g++-$(CONTRIB_GCC_VERSION)
F77=gfortran-$(CONTRIB_GCC_VERSION)
FC=gfortran-$(CONTRIB_GCC_VERSION)
endif
OMPI_CC=$(CC)
export CC
export CXX
export F77
export FC
export OMPI_CC
V=1
export V

#export NVCC_CC = gcc-7
#export NVCC_CC = clang-5.0
export NVCCFLAGS = -Xcompiler -fPIC -Xcompiler -std=c++03

export QT_SELECT=5

ifneq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
CONFIGURE_NODOC = --disable-build-doc
endif

#CONFIGURE_QWT = --with-qwt-include-dir=/usr/include/qwt \
#	--with-qwt-lib=qwt

%:
	dh $@ --parallel $(DH_OPTIONS) --with autoreconf

override_dh_auto_configure:
	dh_auto_configure $(DH_OPTIONS) -- --enable-opengl-render \
		$(CONFIGURE_QWT) \
		--enable-openmp --enable-quick-check --docdir=/usr/share/doc/libstarpu-dev \
		$(CONFIGURE_NODOC)

override_dh_auto_test:
	-HOME=$$PWD DISPLAY= DEB_BUILD_OPTIONS=$(patsubst parallel=%,,$(DEB_BUILD_OPTIONS)) dh_auto_test $(DH_OPTIONS) -k
	$(MAKE) -C build showcheck

#ifeq ($(source),starpu)
#override_dh_install:
#	dh_install usr/lib/starpu/*/gcc/*/*.so* /usr/lib/gcc/$(DEB_HOST_MULTIARCH)/$(GCC_VERSION)/plugin
#endif

override_dh_auto_clean:
	dh_auto_clean $(DH_OPTIONS) 
	rm -fr doc/doxygen/starpu.pdf
	rm -fr doc/doxygen/html
	rm -fr .starpu
	rm -f starpu-top/StarPU-Top
	rm -fr .nv

# This is required by the gcc plugin infrastructure
#GCC_DEBIAN_VERSION   := $(shell dpkg-query -f '$${Version}' -W $(CC))
#GCC_UPSTREAM_VERSION := $(firstword $(subst -, ,$(GCC_DEBIAN_VERSION)))
#override_dh_gencontrol:
#	dh_gencontrol -- -VgccDeps='$(CC) (>= $(GCC_DEBIAN_VERSION)), $(CC) (<< $(GCC_UPSTREAM_VERSION)A)'

# Switch to main rules
main:
	sed -i 's/^starpu-contrib /starpu /' debian/changelog
	sed -i '1s/^Source: starpu-contrib/Source: starpu/' debian/control
	sed -i 's/Package: libstarpu-contrib/Package: libstarpu/' debian/control
	sed -i 's/Package: starpu-contrib/Package: starpu/' debian/control
	sed -i 's/Package: libsocl-contrib/Package: libsocl/' debian/control
	sed -i '/Depends/s/ libstarpu-contrib/ libstarpu/g' debian/control
	sed -i '/Depends/s/ libsocl-contrib/ libsocl/g' debian/control
	sed -i 's/Conflicts: libstarpu/Conflicts: libstarpu-contrib/' debian/control
	sed -i 's/Conflicts: libsocl/Conflicts: libsocl-contrib/' debian/control
	sed -i 's/Conflicts: starpu/Conflicts: starpu-contrib/' debian/control
	sed -i 's/#	gcc-$(GCC_VERSION), g++-$(GCC_VERSION), gfortran-$(GCC_VERSION), gcc-$(GCC_VERSION)-plugin-dev, gcc,$$/	gcc-$(GCC_VERSION), g++-$(GCC_VERSION), gfortran-$(GCC_VERSION), gcc-$(GCC_VERSION)-plugin-dev, gcc,/' debian/control
	sed -i 's/^	gcc-$(CONTRIB_GCC_VERSION), g++-$(CONTRIB_GCC_VERSION), gfortran-$(CONTRIB_GCC_VERSION), gcc-$(CONTRIB_GCC_VERSION)-plugin-dev,$$/#	gcc-$(CONTRIB_GCC_VERSION), g++-$(CONTRIB_GCC_VERSION), gfortran-$(CONTRIB_GCC_VERSION), gcc-$(CONTRIB_GCC_VERSION)-plugin-dev,/' debian/control
	#sed -i 's/^	clang-$(CONTRIB_CLANG_VERSION),$$/#	clang-$(CONTRIB_CLANG_VERSION),/' debian/control
	sed -i 's/^	nvidia-cuda-toolkit/#	nvidia-cuda-toolkit/' debian/control
	#sed -i 's/^Build-Conflicts: gcc-$(DEFAULT_GCC_VERSION), g++-$(DEFAULT_GCC_VERSION)/#Build-Conflicts: gcc-$(DEFAULT_GCC_VERSION), g++-$(DEFAULT_GCC_VERSION)/' debian/control
	sed -i 's_^Section: contrib/_Section: _' debian/control
	sed -i 's/contrib-contrib/contrib/g' debian/control debian/changelog
	sed -i '/starpu-top/,/starpu-examples/s/^#//' debian/control
	sed -i 's/^ This "contrib" version/# This "contrib" version/' debian/control

# Switch to contrib rules
contrib:
	sed -i '/starpu-top/,/^$$/s/^/#/' debian/control
	sed -i 's/^starpu /starpu-contrib /' debian/changelog
	sed -i '1s/^Source: starpu/Source: starpu-contrib/' debian/control
	sed -i 's/Package: libstarpu/Package: libstarpu-contrib/' debian/control
	sed -i 's/Package: starpu/Package: starpu-contrib/' debian/control
	sed -i 's/Package: libsocl/Package: libsocl-contrib/' debian/control
	sed -i '/Depends/s/ libstarpu/ libstarpu-contrib/g' debian/control
	sed -i '/Depends/s/ libsocl/ libsocl-contrib/g' debian/control
	sed -i 's/Conflicts: libstarpu-contrib/Conflicts: libstarpu/' debian/control
	sed -i 's/Conflicts: libsocl-contrib/Conflicts: libsocl/' debian/control
	sed -i 's/Conflicts: starpu-contrib/Conflicts: starpu/' debian/control
	sed -i 's/^	gcc-$(GCC_VERSION), g++-$(GCC_VERSION), gfortran-$(GCC_VERSION), gcc-$(GCC_VERSION)-plugin-dev, gcc,$$/#	gcc-$(GCC_VERSION), g++-$(GCC_VERSION), gfortran-$(GCC_VERSION), gcc-$(GCC_VERSION)-plugin-dev, gcc,/' debian/control
	sed -i 's/#	gcc-$(CONTRIB_GCC_VERSION), g++-$(CONTRIB_GCC_VERSION), gfortran-$(CONTRIB_GCC_VERSION), gcc-$(CONTRIB_GCC_VERSION)-plugin-dev,$$/	gcc-$(CONTRIB_GCC_VERSION), g++-$(CONTRIB_GCC_VERSION), gfortran-$(CONTRIB_GCC_VERSION), gcc-$(CONTRIB_GCC_VERSION)-plugin-dev,/' debian/control
	#sed -i 's/^#	clang-$(CONTRIB_CLANG_VERSION),$$/	clang-$(CONTRIB_CLANG_VERSION),/' debian/control
	sed -i 's/#	nvidia-cuda-toolkit/	nvidia-cuda-toolkit/' debian/control
	#sed -i 's/^#Build-Conflicts: gcc-$(DEFAULT_GCC_VERSION), g++-$(DEFAULT_GCC_VERSION)/Build-Conflicts: gcc-$(DEFAULT_GCC_VERSION), g++-$(DEFAULT_GCC_VERSION)/' debian/control
	sed -i 's_contrib/contrib/_contrib_g' debian/control debian/control
	sed -i 's/contrib-contrib/contrib/g' debian/control debian/changelog
	sed -i 's_^Section: \([^/]*\)$$_Section: contrib/\1_' debian/control
	sed -i 's/^# This "contrib" version/ This "contrib" version/' debian/control
